package com.android.binderserverclient0

import android.app.Application
import android.net.Uri
import android.util.Log
import androidx.work.Configuration
import binderipcservicemanager.AdvancedServiceManagerConstant
import binderipcservicemanager.BCAdvancedServiceManagerProxy
import binderipcservicemanager.IBCAdvancedServiceManager
import com.android.binderserverclient0.mmkv.MMKVLoadLib
import com.tencent.mmkv.MMKV
import com.tencent.mmkv.MMKVContentChangeNotification
import com.tencent.mmkv.MMKVContentProvider
import com.tencent.mmkv.MMKVHandler
import com.tencent.mmkv.MMKVLogLevel
import com.tencent.mmkv.MMKVRecoverStrategic
import java.io.File

class BinderIPCApplication : Application(), Configuration.Provider, MMKVHandler, MMKVContentChangeNotification {

    override fun onCreate() {
        super.onCreate()
        Log.d("Hello00","override fun onCreate()")
        Log.d("BinderServerClient0","class BinderIPCApplication : Application()")

        val bundleResult = contentResolver.call(
            Uri.parse(AdvancedServiceManagerConstant.SERVICE_MANAGER_URI),
            AdvancedServiceManagerConstant.ACTION_ADVANCED_GET_SERVICE,
            null,
            null
        )

        if (bundleResult != null) {
            val binder = bundleResult.getBinder(AdvancedServiceManagerConstant.KEY_ADVANCED_SERVER)
            if (binder != null) {
                mAdvancedServiceManager = BCAdvancedServiceManagerProxy(binder)
            }
        }

        //MMKV.initialize(this,"/sdcard/data012/user/0/com.android.binderserverclient0/files/datastore/mmkv")
        MMKV.initialize(this)
        //val path = MMKV.initialize(this)
        //Log.d("BinderServerClient0","path:${path}")
        //MMKV.initialize(this, MMKVLoadLib())

        //监听内容改变通知
        MMKV.registerContentChangeNotify(this)
    }

    override fun getWorkManagerConfiguration(): Configuration {
        Log.d("Hello00","override fun getWorkManagerConfiguration()")
        return Configuration.Builder().setMinimumLoggingLevel(Log.INFO).setMaxSchedulerLimit(Configuration.MIN_SCHEDULER_LIMIT + 100).build()
    }

    override fun onMMKVCRCCheckFail(mmapID: String?): MMKVRecoverStrategic {
        return MMKVRecoverStrategic.OnErrorRecover
    }

    override fun onMMKVFileLengthError(mmapID: String?): MMKVRecoverStrategic {
        return MMKVRecoverStrategic.OnErrorRecover
    }

    override fun wantLogRedirecting(): Boolean {
        //日志重定向
        return true
    }

    override fun mmkvLog(
        level: MMKVLogLevel?,
        file: String?,
        line: Int,
        function: String?,
        message: String?
    ) {
       //日志打印输出
    }

    override fun onContentChangedByOuterProcess(mmapID: String?) {
        //内容通过外部进程改变
    }

    override fun onTerminate() {
        //MMKV退出
        MMKV.onExit()
        super.onTerminate()
    }

    companion object {
        private var mAdvancedServiceManager: IBCAdvancedServiceManager? = null

        fun getBCServiceManager() : IBCAdvancedServiceManager? {
            return mAdvancedServiceManager
        }
    }

}